﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace DBHelper.DAL
{
	/// <summary>
	/// 数据访问类:Activity
	/// </summary>
	public partial class Activity
	{
		public Activity()
		{}
		#region  Method



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(DBHelper.Model.Activity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Activity(");
			strSql.Append("ActivityCode,ActivityName,ActivityDescription,ActivityType,ActivityContent,VideoPath,PicPath,StartDate,createtime)");
			strSql.Append(" values (");
			strSql.Append("@ActivityCode,@ActivityName,@ActivityDescription,@ActivityType,@ActivityContent,@VideoPath,@PicPath,@StartDate,@createtime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@ActivityCode", SqlDbType.NChar,10),
					new SqlParameter("@ActivityName", SqlDbType.NChar,30),
					new SqlParameter("@ActivityDescription", SqlDbType.NChar,200),
					new SqlParameter("@ActivityType", SqlDbType.NChar,15),
					new SqlParameter("@ActivityContent", SqlDbType.NVarChar),
					new SqlParameter("@VideoPath", SqlDbType.NChar,50),
					new SqlParameter("@PicPath", SqlDbType.NChar,50),
					new SqlParameter("@StartDate", SqlDbType.DateTime),
					new SqlParameter("@createtime", SqlDbType.DateTime)};
			parameters[0].Value = model.ActivityCode;
			parameters[1].Value = model.ActivityName;
			parameters[2].Value = model.ActivityDescription;
			parameters[3].Value = model.ActivityType;
			parameters[4].Value = model.ActivityContent;
			parameters[5].Value = model.VideoPath;
			parameters[6].Value = model.PicPath;
			parameters[7].Value = model.StartDate;
			parameters[8].Value = model.createtime;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DBHelper.Model.Activity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Activity set ");
			strSql.Append("ActivityCode=@ActivityCode,");
			strSql.Append("ActivityName=@ActivityName,");
			strSql.Append("ActivityDescription=@ActivityDescription,");
			strSql.Append("ActivityType=@ActivityType,");
			strSql.Append("ActivityContent=@ActivityContent,");
			strSql.Append("VideoPath=@VideoPath,");
			strSql.Append("PicPath=@PicPath,");
			strSql.Append("StartDate=@StartDate,");
			strSql.Append("createtime=@createtime");
			strSql.Append(" where AcitivityNum=@AcitivityNum");
			SqlParameter[] parameters = {
					new SqlParameter("@ActivityCode", SqlDbType.NChar,10),
					new SqlParameter("@ActivityName", SqlDbType.NChar,30),
					new SqlParameter("@ActivityDescription", SqlDbType.NChar,200),
					new SqlParameter("@ActivityType", SqlDbType.NChar,15),
					new SqlParameter("@ActivityContent", SqlDbType.NVarChar),
					new SqlParameter("@VideoPath", SqlDbType.NChar,50),
					new SqlParameter("@PicPath", SqlDbType.NChar,50),
					new SqlParameter("@StartDate", SqlDbType.DateTime),
					new SqlParameter("@createtime", SqlDbType.DateTime),
					new SqlParameter("@AcitivityNum", SqlDbType.SmallInt,2)};
			parameters[0].Value = model.ActivityCode;
			parameters[1].Value = model.ActivityName;
			parameters[2].Value = model.ActivityDescription;
			parameters[3].Value = model.ActivityType;
			parameters[4].Value = model.ActivityContent;
			parameters[5].Value = model.VideoPath;
			parameters[6].Value = model.PicPath;
			parameters[7].Value = model.StartDate;
			parameters[8].Value = model.createtime;
			parameters[9].Value = model.AcitivityNum;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int AcitivityNum)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Activity ");
			strSql.Append(" where AcitivityNum=@AcitivityNum");
			SqlParameter[] parameters = {
					new SqlParameter("@AcitivityNum", SqlDbType.SmallInt)
			};
			parameters[0].Value = AcitivityNum;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string AcitivityNumlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Activity ");
			strSql.Append(" where AcitivityNum in ("+AcitivityNumlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DBHelper.Model.Activity GetModel(int AcitivityNum)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 AcitivityNum,ActivityCode,ActivityName,ActivityDescription,ActivityType,ActivityContent,VideoPath,PicPath,StartDate,createtime from Activity ");
			strSql.Append(" where AcitivityNum=@AcitivityNum");
			SqlParameter[] parameters = {
					new SqlParameter("@AcitivityNum", SqlDbType.SmallInt)
			};
			parameters[0].Value = AcitivityNum;

			DBHelper.Model.Activity model=new DBHelper.Model.Activity();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["AcitivityNum"]!=null && ds.Tables[0].Rows[0]["AcitivityNum"].ToString()!="")
				{
					model.AcitivityNum=int.Parse(ds.Tables[0].Rows[0]["AcitivityNum"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ActivityCode"]!=null && ds.Tables[0].Rows[0]["ActivityCode"].ToString()!="")
				{
					model.ActivityCode=ds.Tables[0].Rows[0]["ActivityCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ActivityName"]!=null && ds.Tables[0].Rows[0]["ActivityName"].ToString()!="")
				{
					model.ActivityName=ds.Tables[0].Rows[0]["ActivityName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ActivityDescription"]!=null && ds.Tables[0].Rows[0]["ActivityDescription"].ToString()!="")
				{
					model.ActivityDescription=ds.Tables[0].Rows[0]["ActivityDescription"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ActivityType"]!=null && ds.Tables[0].Rows[0]["ActivityType"].ToString()!="")
				{
					model.ActivityType=ds.Tables[0].Rows[0]["ActivityType"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ActivityContent"]!=null && ds.Tables[0].Rows[0]["ActivityContent"].ToString()!="")
				{
					model.ActivityContent=ds.Tables[0].Rows[0]["ActivityContent"].ToString();
				}
				if(ds.Tables[0].Rows[0]["VideoPath"]!=null && ds.Tables[0].Rows[0]["VideoPath"].ToString()!="")
				{
					model.VideoPath=ds.Tables[0].Rows[0]["VideoPath"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PicPath"]!=null && ds.Tables[0].Rows[0]["PicPath"].ToString()!="")
				{
					model.PicPath=ds.Tables[0].Rows[0]["PicPath"].ToString();
				}
				if(ds.Tables[0].Rows[0]["StartDate"]!=null && ds.Tables[0].Rows[0]["StartDate"].ToString()!="")
				{
					model.StartDate=DateTime.Parse(ds.Tables[0].Rows[0]["StartDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["createtime"]!=null && ds.Tables[0].Rows[0]["createtime"].ToString()!="")
				{
					model.createtime=DateTime.Parse(ds.Tables[0].Rows[0]["createtime"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select AcitivityNum,ActivityCode,ActivityName,ActivityDescription,ActivityType,ActivityContent,VideoPath,PicPath,StartDate,createtime ");
			strSql.Append(" FROM Activity ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" AcitivityNum,ActivityCode,ActivityName,ActivityDescription,ActivityType,ActivityContent,VideoPath,PicPath,StartDate,createtime ");
			strSql.Append(" FROM Activity ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Activity ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.AcitivityNum desc");
			}
			strSql.Append(")AS Row, T.*  from Activity T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Activity";
			parameters[1].Value = "AcitivityNum";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

